"""circular_linked_list.py
循环链表（Circular Linked List）是一种特殊的链表。在循环链表中，最后一个元素指向头部元素，形成一个闭环。这种数据结构特别适合需要持续循环处理数据的场景。常见的循环链表操作包括添加元素（add）、删除元素（remove）、获取元素（get）、链表是否为空（is_empty）、链表的长度（size）等。

以下是使用 Python 实现的一个简单循环链表。循环链表类将包含以下操作：添加元素(add)、删除元素(remove)、获取元素(get)、链表是否为空(is_empty)、链表的长度(size)。
"""

class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None

class CircularLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def is_empty(self):
        return self.head is None

    def add(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            self.tail = new_node
            self.tail.next = self.head
        else:
            self.tail.next = new_node
            self.tail = new_node
            self.tail.next = self.head

    def size(self):
        if self.head is None:
            return 0
        node = self.head
        count = 1
        while node.next != self.head:
            count += 1
            node = node.next
        return count

    def get(self, index):
        if self.head is None or index < 0:
            return None
        node = self.head
        for _ in range(index):
            node = node.next
            if node == self.head:
                return None
        return node.data

    def remove(self, data):
        if self.head is None:
            return False
        if self.head.data == data:
            current = self.head
            while current.next != self.head:
                current = current.next
            self.head = self.head.next
            current.next = self.head
            return True
        current = self.head
        while current.next != self.head:
            if current.next.data == data:
                current.next = current.next.next
                return True
            current = current.next
        return False

    def print(self):
        if self.head is None:
            print("List is empty")
        else:
            node = self.head
            print(node.data, end=' ')
            while node.next != self.head:
                node = node.next
                print(node.data, end=' ')
            print()


if __name__ == '__main__':
    cll = CircularLinkedList()
    print(cll.is_empty())  # True

    cll.add(1)
    cll.add(2)
    cll.add(3)
    print(cll.size())  # 3
    cll.print()  # Outputs: 1 2 3

    print(cll.get(1))  # 2

    cll.remove(2)
    print(cll.get(1))  # 3

    print(cll.size())  # 2
    cll.print()  # Outputs: 1 3
